package com.albatross.rest;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class JsonToTable {

	private Connection connect = null;
	PreparedStatement preparedStatement = null;
	static final String DB_URL = "jdbc:oracle:thin:@ma-shieldd-ldb01.corp.apple.com:1546:shield1d";
	// Database credentials
	static final String USER = "SHIELD_USER";
	static final String PASS = "passwordtemppasswordtemp4";

	public static void main(String[] args) {
		String test = new JsonToTable()
				.callURL("https://isdb-api-java-test.corp.apple.com/api/enriched/host/query?vulnerability");

		try {
			//new JsonToTable().save(test);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}


	
	public String callURL(String myURL) {
		System.out.println("Requeted URL:" + myURL);
		StringBuilder sb = new StringBuilder();
		URLConnection urlConn = null;
		InputStreamReader in = null;
		try {
			URL url = new URL(myURL);
			urlConn = url.openConnection();
			if (urlConn != null)
				urlConn.setReadTimeout(60 * 10000);
			if (urlConn != null && urlConn.getInputStream() != null) {
				in = new InputStreamReader(urlConn.getInputStream(),
						Charset.defaultCharset());
				BufferedReader bufferedReader = new BufferedReader(in);
				if (bufferedReader != null) {
					int cp;
					while ((cp = bufferedReader.read()) != -1) {
						sb.append((char) cp);
					}
					bufferedReader.close();
				}
			}
			in.close();

		} catch (Exception e) {
			throw new RuntimeException("Exception while calling URL:" + myURL,
					e);
		}

	 System.out.println("\nOutput: \n" + sb.toString());

		return sb.toString();

	}

	public void save(String stringJson) throws Exception {
		int status = 0;
		try {
			// Load the Oracle driver, each DB has its own driver
			Class.forName("oracle.jdbc.driver.OracleDriver");

			// DB connection setup
			System.out.println("Connecting to database...");
			connect = DriverManager.getConnection(DB_URL, USER, PASS);

			if (connect != null) {
				System.out.println("connected successfully");
			}
			// PreparedStatements
			preparedStatement = connect
					.prepareStatement("insert into  test_vuln_master1(vul_Id,App_Ip)"
							+ "values (?,?)");

			JSONParser parser = new JSONParser();
			String app_id = null;
			long owner_id;
			String ip = null, role = null, name = null, email = null;

			try {
				Object obj = parser.parse(stringJson.toString());
				JSONObject mainArray = (JSONObject) obj;
				JSONArray jsonMainArr = (JSONArray) mainArray.get("results");

				System.out.println("total size" + jsonMainArr.size());
				for (int i = 0; i < jsonMainArr.size(); i++) { // **line 2**

					JSONObject childJSONObject = (JSONObject) jsonMainArr
							.get(i);
					
					
					JSONObject jsonSubArrayJSONObject = (JSONObject) parser
							.parse(childJSONObject.get("bigindex").toString());


				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("insertion completed");
		} catch (Exception e) {
			throw e;
		} finally {
			try {
				if (connect != null) {
					connect.close();
				}

			} catch (Exception e) {

			}
		}
	}

	public String callFile(String myFile) {
		System.out.println("Requested File:" + myFile);
		StringBuilder stringJson = new StringBuilder();

		try {


		FileReader logReader = new FileReader(myFile);
		BufferedReader buffer = new BufferedReader(logReader);
		if (buffer != null) {
		int cp;
		while ((cp = buffer.read()) != -1) {
		stringJson.append((char) cp);
		}
		buffer.close();
		} }



		 catch (Exception e) {
		throw new RuntimeException("Exception while reading File:" + myFile,
		e);
		 }



		return stringJson.toString();

		}
}
